
#include "GButton.h"
#include "GWindowMgr.h"
//#include "GML.h"

gint onBtMotion( GtkWidget      *widget, 
			  GdkEventMotion *event,
			  gpointer        data );


gboolean onBtLeave_notify_event(GtkWidget	     *widget,
							  GdkEventCrossing    *event);

gboolean onButton_Press_event(GtkWidget	     *widget,
							  GdkEventCrossing    *event);
gboolean onButton_Release_event(GtkWidget	     *widget,
							  GdkEventCrossing    *event);

/* Expose callback for the drawing area */
static gint
expose_cb (GtkWidget	  *widget,
		   GdkEventExpose *event,
		   gpointer	   data)
{

	return TRUE;
}

CGButton::CGButton(void)
{
	m_eState = E_BS_Normal;

	m_pLabel = NULL;
	m_pButton = NULL;
	m_pDisableImage = NULL;
	m_pPressImage   = NULL;
	m_pHoverImage   = NULL;
	m_pNormalImage  = NULL;
}

CGButton::~CGButton(void)
{
	
}

void CGButton::show()
{

	hide();

	//E_BS_Press,
	//	E_BS_Hover,
	//	E_BS_Normal,
	//	E_BS_Disable

	gtk_widget_show( m_pButton );
	gtk_widget_show( m_pLabel );

	switch(m_eState)
	{
	case E_BS_Press:
		gtk_widget_show( m_pPressImage );
		break;
	case E_BS_Hover:
		gtk_widget_show( m_pHoverImage );

		break;
	case E_BS_Normal:
		gtk_widget_show( m_pNormalImage );

		break;
	case E_BS_Disable:
		gtk_widget_show( m_pDisableImage );

		break;
	default:
		break;
	}
}

void CGButton::hide()
{

	gtk_widget_hide( m_pButton );
	gtk_widget_hide( m_pPressImage );
	gtk_widget_hide( m_pHoverImage );
	gtk_widget_hide( m_pNormalImage );
	gtk_widget_hide( m_pDisableImage );
	gtk_widget_hide( m_pLabel );


}

void CGButton::create(const char* bg_file)
{
	m_pButton = gtk_button_new_with_label( "" );
	gtk_widget_show( m_pButton );
	//gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_file(filename));
	//gtk_button_set_image( GTK_BUTTON(m_pButton), m_pNormalImage );
	g_signal_connect (G_OBJECT (m_pButton), "enter_notify_event",
		G_CALLBACK (onBtMotion), NULL);

	g_signal_connect (G_OBJECT (m_pButton), "leave_notify_event",
		G_CALLBACK (onBtLeave_notify_event), NULL);

	g_signal_connect (G_OBJECT (m_pButton), "button_release_event",
		G_CALLBACK (onButton_Release_event), NULL);
	g_signal_connect (G_OBJECT (m_pButton), "button_press_event",
		G_CALLBACK (onButton_Press_event), NULL);

	g_signal_connect (m_pButton, "expose_event",
		G_CALLBACK (expose_cb), NULL);

	regWindow();
}

void CGButton::setPosition( int x, int y)
{
	gtk_fixed_put(GTK_FIXED (m_pFixed), m_pButton, x, y);
	setImagePos( x, y );
}


void CGButton::setSize( int x, int y)
{
	gtk_widget_set_size_request (m_pButton, x, y);

	setImageSize( x, y );
}

CGPoint CGButton::getPosition()
{
	return CGPoint( 1, 1);
}

CGPoint CGButton::size()
{
	return CGPoint( 1, 1);
}

void CGButton::setImageSizePos( int w, int h, int x, int y)
{
	setImagePos( x, y );
	setImageSize( w, h );
}

void CGButton::setImagePos( int x, int y)
{
	gtk_fixed_put (GTK_FIXED (m_pFixed), m_pPressImage, x, y);
	gtk_fixed_put (GTK_FIXED (m_pFixed), m_pHoverImage, x, y);
	gtk_fixed_put (GTK_FIXED (m_pFixed), m_pNormalImage, x, y);
	gtk_fixed_put (GTK_FIXED (m_pFixed), m_pDisableImage, x, y);

}

void CGButton::setImageSize( int w, int h)
{
	//gtk_image_update_size (image, gdk_image->width, gdk_image->height);
	gtk_widget_set_usize(m_pPressImage,w,h);
	gtk_widget_set_usize(m_pHoverImage,w,h);
	gtk_widget_set_usize(m_pNormalImage,w,h);
	gtk_widget_set_usize(m_pDisableImage,w,h);

	gtk_widget_queue_resize (GTK_WIDGET (m_pNormalImage));


	//gtk_widget_set_size_request (m_pPressImage, w, h);
	//gtk_widget_set_size_request (m_pHoverImage, w, h);
	//gtk_widget_set_size_request (m_pNormalImage, w, h);
	//gtk_widget_set_size_request (m_pDisableImage, w, h);
}


void CGButton::advanceTime( float fElaspedTime )
{
	
}

bool CGButton::isPari( GtkWidget *_pGw)
{
	if ( _pGw == m_pButton )
		return true;
	else
		return false;
}

void CGButton::_stateUpdate( E_ButtonState _state)
{

	//E_BS_Press,
	//	E_BS_Hover,
	//	E_BS_Normal,
	//	E_BS_Disable

	switch( _state )
	{
	case E_BS_Disable:
		{
			m_eState = E_BS_Disable;
			gtk_widget_hide( m_pHoverImage );
			gtk_widget_hide( m_pNormalImage );
			gtk_widget_hide( m_pPressImage );

			gtk_widget_show( m_pDisableImage );

		}
		break;
	case E_BS_Normal:
		{
			if ( E_BS_Hover == m_eState )
			{
				m_eState = E_BS_Normal;
				gtk_widget_hide( m_pHoverImage );
				gtk_widget_show( m_pNormalImage );

				return;
			}
			else if ( E_BS_Press == m_eState )
			{
				m_eState = E_BS_Normal;
				gtk_widget_hide( m_pPressImage );
				gtk_widget_show( m_pNormalImage );

				return;
			}
			else if ( E_BS_Disable == m_eState )
			{
				m_eState = E_BS_Normal;
				gtk_widget_hide( m_pDisableImage );
				gtk_widget_show( m_pNormalImage );

				return;
			}
		}
		break;
	case E_BS_Hover:
		{
			if ( E_BS_Normal == m_eState )
			{
				m_eState = E_BS_Hover;
				gtk_widget_show( m_pHoverImage );
				gtk_widget_hide( m_pNormalImage );

				return;
			}
			else if ( E_BS_Press == m_eState )
			{
				m_eState = E_BS_Press;
				gtk_widget_hide( m_pPressImage );
				gtk_widget_show( m_pHoverImage );

				return;
			}

		}
		break;
	case E_BS_Press:
		{
			if ( E_BS_Normal == m_eState )
			{
				m_eState = E_BS_Hover;
				gtk_widget_show( m_pPressImage );
				gtk_widget_hide( m_pNormalImage );

				return;
			}
			else if ( E_BS_Hover == m_eState )
			{
				m_eState = E_BS_Hover;
				gtk_widget_hide( m_pHoverImage );
				gtk_widget_show( m_pPressImage );

				return;
			}
		}
		break;

	default:
		break;
	}
}

void CGButton::press()
{
	if ( !isEnabled())
		return;
	_stateUpdate( E_BS_Press );
}


void CGButton::release()
{
	if ( !isEnabled())
		return;
	_stateUpdate( E_BS_Normal );
	G_MSG_CLICK();
}

void CGButton::hover()
{
	if ( !isEnabled())
		return;
	G_MSG_HOVER();
	_stateUpdate( E_BS_Hover );

}

void CGButton::leave()
{
	if ( !isEnabled())
		return;
	_stateUpdate( E_BS_Normal );
	G_MSG_LEAVE();

}

gint onBtMotion( GtkWidget      *widget, 
			  GdkEventMotion *event,
			  gpointer        data )
{
	CGButton * pButton = (CGButton *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	if( pButton )
		pButton->hover();

	////	g_print ("Hello again -  X : %f, Y: %f\n", event->x, event->y );

	return 1;
}

gboolean onBtLeave_notify_event(GtkWidget	     *widget,
							  GdkEventCrossing    *event)
{
	//g_print ("Hello again -  X : %f, Y: %f\n", event->x, event->y );

	//CGTabControl * pTabCtl = (CGTabControl *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	//if( pTabCtl )
	//	pTabCtl->normalUnSelectedLabel(  );

	CGButton * pButton = (CGButton *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	if( pButton )
		pButton->leave();

	return 1;
}

gboolean onButton_Press_event(GtkWidget	     *widget,
							  GdkEventCrossing    *event)
{
	CGButton * pButton = (CGButton *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	if( pButton )
		pButton->press();
	return 1;
}

gboolean onButton_Release_event(GtkWidget	     *widget,
								GdkEventCrossing    *event)
{
	CGButton * pButton = (CGButton *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	if( pButton )
		pButton->release();
	return 1;
}

void CGButton::createNormalImg( const gchar *filename )
{
	m_pNormalImage = create_pixmap (m_pButton, filename);

}

void CGButton::createPressImg(  const gchar *filename )
{
	m_pPressImage = create_pixmap (m_pButton, filename);

}


void CGButton::createHoverImg(  const gchar *filename )
{
	m_pHoverImage = create_pixmap (m_pButton, filename);

}


void CGButton::createDisableImg(  const gchar *filename )
{
	m_pDisableImage = create_pixmap (m_pButton, filename);

}

void CGButton::enabled()
{
	m_bEnabled = true;
	_stateUpdate( E_BS_Normal );

}

void CGButton::disable()
{
	m_bEnabled = false;
	_stateUpdate( E_BS_Disable );
	
	
}

void CGButton::updateLanguage()
{
	string temp = m_strOri.c_str();

	gtk_label_set_label( GTK_LABEL(m_pLabel),temp.c_str() );
}
